<template>
<div class="address">
  <Header><span slot="back" class="back" @click="goback" >返回</span>
    <span slot="home" class="home" @click="gohome">首页</span></Header>
    <van-address-edit
    :area-list="areaList"
    show-postal
    show-delete
    show-set-default
    show-search-result
    :address-info="info"
    :search-result="searchResult"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
    @change-detail="onChangeDetail"
    />
</div>
</template>
<script>
import Header from "../../components/HeaderPublic";
import { Toast } from "vant";
import { areaList } from "@vant/area-data"; // 是级联数据
import {reqinitaddress,reqEditaddress,reqDeladdress} from "../../api/reqaddress";
import {reqshowAddress,reqSubmitorder} from "../../api/reqshowaddress";
export default {
data() {
return {
    areaList,
    searchResult: [],
    info:{},
    list:[],//获取所有的地址
};
},
computed: {},
watch: {},
methods: {
  //初始化地址信息
    async initaddress(){
      const result = await reqshowAddress();

      result.addresses.forEach(item => {
        let {_id,receiver,mobile,regions,address,isDefault} = item;
        this.list.push({
          id:_id,
          name: receiver,
          tel: mobile,
          address: regions + address,
          isDefault: isDefault,
          ssq:regions,
          xxdz:address
        });
      });
    },
    //
    async initdata(id) {
      var str = "";
      var strarr = [];
      var code = "";
      const result = await reqinitaddress(id);

      str = result.regions;
      strarr = str.split(" ");
      for (var key in this.areaList.county_list){
        if(strarr[2] == this.areaList.county_list[key]){
          code = key;
        }
      };
      var {_id,receiver,mobile,regions,address,isDefault} = result;
      this.info={
        id:_id,
        name:receiver,
        tel:mobile,
        province: strarr[0],
        city: strarr[1],
        county: strarr[2],
        addressDetail:address,
        areaCode: code,
        postalCode:"000000",
        isDefault:isDefault
      }
    },
    //封装接口
    async updateaddress(id,name,tel,ssq,xxdz,defaults){
    const result =  await reqEditaddress(id,{
                 receiver:name,
                 mobile:tel,
                 regions:ssq,
                 address:xxdz,
                 isDefault:defaults,
           });
           console.log(result);
    },
    //保存按钮事件
    async onSave(content) {
      Toast('修改成功');
      const id = this.$route.params.id;
      const result = await reqEditaddress(id,{
        receiver:content.name,
        mobile:content.tel,
        regions:content.province + " " + content.city + " " + content.county,
        address:content.addressDetail,
        isDefault:content.isDefault
      });
      console.log(content);
      //取消其它地址默认
      if(content.isDefault == true){
        console.log(1111);
        this.list.filter((element,indexs)=>{return element.id!=id;}).forEach((items)=>{
          //取消其它默认项
        if(items.isDefault == true){ 
          console.log(2222);
           this.updateaddress(items.id,items.name,items.tel,items.ssq,items.xxdz,false);

        }
        });
      }
     this.$router.push("/showaddress");
    },
    //
    async onDelete() {
      Toast('删除成功');
      const id = this.$route.params.id;
      const result = await reqDeladdress(id);
      console.log(result);
      this.$router.push("/showaddress");
    },
    //
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
    goback(){
        this.$router.back();
    },
    gohome(){
        this.$router.push("/home");
    },
},
//
created() {
  const id = this.$route.params.id;
  this.initdata(id);
 this. initaddress();
},
mounted() {},
components: {
  Header,
},
};
</script>
<style scoped>
</style>
